package com.woniuxy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Room;
import com.woniuxy.entity.RoomDto;
import com.woniuxy.service.RoomService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Room)表控制层
 *
 * @author makejava
 * @since 2021-05-15 14:52:39
 */
@RestController
@RequestMapping("room")
public class RoomController {
    /**
     * 服务对象
     */
    @Resource
    private RoomService roomService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Room selectOne(Integer id) {
        return this.roomService.queryById(id);
    }


    /**
     * Description : 根据前端发送的页码，和每页数据查询所有数据  <br/>
     * @param startpage
     * @param pagesize
     * @return com.github.pagehelper.PageInfo<com.woniuxy.entity.Room>
    */

    @GetMapping("selects")
    public PageInfo<Room> selects(int startpage, int pagesize){
        PageHelper.startPage(startpage, pagesize);
        Page<Room> page = (Page<Room>)this.roomService.selects();
        PageInfo<Room> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }


    /**
     * Description : 模糊查询  <br/>
     * @param roomDto
     * @return com.github.pagehelper.PageInfo<com.woniuxy.entity.Room>
    */
    @PostMapping("search")
    private PageInfo<Room> search(@RequestBody RoomDto roomDto){
        PageHelper.startPage(roomDto.getPage(),roomDto.getLimit());
        Page<Room> page = (Page<Room>) this.roomService.queryAll(roomDto.getRoom());
        PageInfo<Room> pageInfo = new PageInfo<>(page);
        return pageInfo;

    }

    /**
     * Description : 增加  <br/>
     * @param room
     * @return string
    */
    @PostMapping("insert")
    public String insert(@RequestBody Room room){
        int i = this.roomService.insert(room);
        if (i>0){
            return "success";
        }
        return "failure";
    }

    /**
     * Description : 修改  <br/>
     * @param room
     * @return string
    */

    @PostMapping("update")
    public String update(@RequestBody Room room){
        int i = this.roomService.update(room);
        if (i>0){
            return "success";
        }
        return "failure";
    }

    /**
     * Description : 删除  <br/>
     * @return string
    */

    @PostMapping("delete/{roomId}")
    public String delete(@PathVariable("roomId") int roomId){
        boolean b = this.roomService.deleteById(roomId);
        if (b) {
            return "success";
        }
        return "failure";
    }


    /**
     * Auther: zxy <br/>
     * Date: 15:58:2021/5/22  <br/>
     * Description:传送给后端房间数据
     */
    @ApiOperation("获取房间")
    @GetMapping("selectAll")
    public ResponseEntity<Room> selectAll(){
        List<Room> selects = this.roomService.selects();
        if (selects!=null){
            return new ResponseEntity(1,"查询房间数据成功", selects);
        }
        return new ResponseEntity(0,"查询房间数据失败");

    }

    /**
     * 
     * Date: 16:49:2021/5/28  <br/>
     * Description:根据房间id更变房间状态
     */
    @PostMapping("changeRoomStatus")
    @ApiOperation("改变房间状态")
    public ResponseEntity changeRoomStatus(@RequestBody Room room){
        int update = this.roomService.update(room);
        if (update > 0){
            return new ResponseEntity(1,"改变房间状态成功");
        }
        return new ResponseEntity(0,"改变房间状态失败");
    }
}
